package com.example.springbott.wx_service;

import com.example.springbott.bean.MarketFootprint;
import com.example.springbott.bean.MarketFootprintExample;
import com.example.springbott.bean.MarketGoods;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.silic.MarketGoodsListVo;
import com.example.springbott.mapper.MarketFootprintMapper;
import com.example.springbott.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 个人足迹
 *
 * @author silic xia
 * @since 2022/11/22 15:55
 */
@Service
public class WXFootprintServiceImpl implements WXFootprintService {

    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     *  list
     * @param /wx/footprint/list?page=1&limit=10
     * @return BaseRespVo--CommonData--MarketGoodsListVo
     * @author silic xia
     * @since 2022/11/22 15:24
     */
    @Override
    public CommonData<MarketGoodsListVo> wxFootprintList(BasePageInfo info, Integer id) {
        PageHelper.startPage(info.getPage(),info.getLimit());

        // 选择该id的footprint
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        example.setOrderByClause("add_time" + " " + "desc");

        // 包装数据
        ArrayList<MarketGoodsListVo> list = new ArrayList<>();
        List<MarketFootprint> marketFootprintList = footprintMapper.selectByExample(example);
        for (MarketFootprint fo : marketFootprintList) {
            // goods信息、footprint信息
            MarketGoods goods = goodsMapper.selectByPrimaryKey(fo.getGoodsId());
            MarketGoodsListVo vo
                    = new MarketGoodsListVo(goods.getBrief(),goods.getPicUrl(),fo.getAddTime(),fo.getGoodsId(),goods.getName(),fo.getId(),goods.getRetailPrice());
            list.add(vo);
        }
        PageInfo pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     *  delete
     *
     * @param /{"id":198}
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/22 15:24
     */
    @Override
    public void wxFootprintDelete(Integer footprintId,Integer userId) {
        MarketFootprint footprint = new MarketFootprint();
        footprint.setDeleted(true);
        footprint.setUpdateTime(new Date());

        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(footprintId);
        criteria.andUserIdEqualTo(userId);
        footprintMapper.updateByExampleSelective(footprint,example);
    }


}

